home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / fsattach / exec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-08  |  3.1 KB  |  152 lines

  1. /* 
  2.  * exec.c --
  3.  *
  4.  *    Routines that provide a nice interface for execing routines with a
  5.  *    variable number of parameters.
  6.  *
  7.  * Copyright 1989 Regents of the University of California
  8.  * Permission to use, copy, modify, and distribute this
  9.  * software and its documentation for any purpose and without
  10.  * fee is hereby granted, provided that the above copyright
  11.  * notice appear in all copies.  The University of California
  12.  * makes no representations about the suitability of this
  13.  * software for any purpose.  It is provided "as is" without
  14.  * express or implied warranty.
  15.  */
  16.  
  17. #ifndef lint
  18. static char rcsid[] = "$Header: /sprite/src/admin/fsattach/RCS/exec.c,v 1.3 89/06/07 22:14:25 jhh Exp $ SPRITE (Berkeley)";
  19. #endif /* not lint */
  20.  
  21. #include "fsattach.h"
  22. #include <varargs.h>
  23.  
  24.  
  25. static char     *argArray[MAX_EXEC_ARGS];
  26. static int    argCount;
  27. static char    *routineName;
  28. /*
  29.  *----------------------------------------------------------------------
  30.  *
  31.  * StartExec --
  32.  *
  33.  *    Initialize data structures for doing an exec. The routine name must
  34.  *    point to an area of storage that is not modified until DoExec()
  35.  *    is called.
  36.  *
  37.  * Results:
  38.  *    None.
  39.  *
  40.  * Side effects:
  41.  *    Arg count is set to 1, routine name stored and first arg stored.
  42.  *
  43.  *----------------------------------------------------------------------
  44.  */
  45.  
  46. void
  47. StartExec(routine, firstArg)
  48.     char    *routine;
  49.     char    *firstArg;
  50. {
  51.     routineName = routine;
  52.     argArray[0] = firstArg;
  53.     argCount = 1;
  54. }
  55.  
  56. /*
  57.  *----------------------------------------------------------------------
  58.  *
  59.  * AddExecArgs --
  60.  *
  61.  *    The list of arguments is added to the array to be passed to exec.
  62.  *    The storage used by the arguments must not be modified until
  63.  *    DoExec is called.
  64.  *
  65.  * Results:
  66.  *    None.
  67.  *
  68.  * Side effects:
  69.  *    The argument array and arg count are modified.
  70.  *
  71.  *----------------------------------------------------------------------
  72.  */
  73. #ifdef lint
  74. /*VARARGS*/
  75. /*ARGSUSED*/
  76. void
  77. AddExecArgs(foo)
  78.     char    *foo;
  79. {}
  80.  
  81. #else
  82.  
  83. void
  84. AddExecArgs(va_alist)
  85.     va_dcl
  86.  
  87. {
  88.     char    *string;
  89.  
  90.     va_list    args;
  91.  
  92.     va_start(args);
  93.     for(string = va_arg(args, char *);
  94.     string != NULL; 
  95.     string = va_arg(args, char *), argCount++) {
  96.  
  97.     if (argCount >= MAX_EXEC_ARGS) {
  98.         (void) fprintf(stderr, "Too many args to exec.\n");
  99.         return;
  100.     }
  101.     argArray[argCount] = string;
  102.     }
  103. }
  104. #endif
  105.  
  106. /*
  107.  *----------------------------------------------------------------------
  108.  *
  109.  * DoExec --
  110.  *
  111.  *    Calls Exec with the arguments built up so far.
  112.  *
  113.  * Results:
  114.  *    Process id of child process, -1 if there was an error.
  115.  *
  116.  * Side effects:
  117.  *    Child process is forked.
  118.  *
  119.  *----------------------------------------------------------------------
  120.  */
  121.  
  122. int
  123. DoExec()
  124. {
  125.     int            pid;
  126.     int            i;
  127.  
  128.     if (argCount >= MAX_EXEC_ARGS) {
  129.     (void) fprintf(stderr, "Too many args to exec.\n");
  130.     return -1;
  131.     }
  132.     argArray[argCount] = NULL;
  133.     if (verbose) {
  134.     for (i = 0; i < argCount; i++) {
  135.         printf("%s ", argArray[i]);
  136.     }
  137.     putchar('\n');
  138.     }
  139.     if (printOnly) {
  140.     return 0;
  141.     }
  142.     pid = fork();
  143.     if (pid == 0) {
  144.     (void) execvp(routineName, argArray);
  145.     (void) fprintf(stderr, "Exec failed.\n");
  146.     (void) perror("");
  147.     (void) _exit(EXEC_FAILED);
  148.     } 
  149.     return pid;
  150. }
  151.  
  152.